package Comprehensive_03;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Demo6 {
    /*
        某商店想统计一下一天内所售出的商品以及商品的数量，
        请设计程序完成此需求,展示每个售出的商品名称和数量
        要求:通过键盘录入商品名称模拟售出的商品，
        录入一次表示商品售出一次，直到录入end结束,
        结束之后,将统计结果打印出来
     */
    static Scanner sc = new Scanner(System.in);
    static Map<String, Integer> goods = new HashMap<>();
    static Map<String, Integer> goodsSell = new HashMap<>();
    static {
        goods.put("小浣熊", 5);
        goods.put("口香糖", 4);
        goods.put("香蕉", 6);
        goods.put("苹果", 7);
        goods.put("辣条", 8);
    }
    public static void main(String[] args) {
        buy();
    }

    private static void buy() {
        while (true) {
            System.out.println("请输入商品名称,每行输入一个,以end结束");
            String name = sc.next();
            if (goods.containsKey(name)) {
                Integer i = goods.get(name);
                if (i <= 0){
                    System.out.println("您要购买的商品已经买完~");
                    return;
                }
                System.out.println("购买成功!");
                goods.put(name,i - 1);
                if (goodsSell.containsKey(name)){
                    Integer j = goodsSell.get(name);
                    goodsSell.put(name, j);
                } else {
                    goodsSell.put(name,1);
                }
            } else if ("end".equals(name)){
                for (String s : goodsSell.keySet()) {
                    System.out.println(s + "售出数量:" + goodsSell.get(s));
                }
                return;
            } else {
                System.out.println("您要购买的商品不存在,请重新选择");
            }
        }
    }

}
